﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TesterLibrary
{
    public class Exam
    {
        protected int numPossibleQuestions;
        protected int numQuestionsInExam;
        protected bool questionsInRandomOrder;
        protected bool answersInRandomOrder;

        protected List<ExamQuestion> examQuestions;

        #region Properties
        public bool RandomizeQuestions
        {
            get { return questionsInRandomOrder; }
            set { questionsInRandomOrder = value; }
        }

        public bool RandomizeAnswers
        {
            get { return answersInRandomOrder; }
            set { answersInRandomOrder = value; }
        }
        #endregion

        public Exam()
        {
            numPossibleQuestions = 0;
            numQuestionsInExam = 20;
            questionsInRandomOrder = false;
            answersInRandomOrder = false;
            examQuestions = new List<ExamQuestion>();
        }

        /// <summary>
        /// Construct the exam from the specified xml file containing the question and answer data.
        /// </summary>
        /// <param name="xmlFilename"></param>
        public Exam(string xmlFilename)
            : this()
        {
            
        }

        /// <summary>
        /// Move to the next question in the list
        /// (Stores the currently selected user answer and moves to the next question in the list)
        /// </summary>
        public void NextQuestion()
        {
        }

        /// <summary>
        /// Move to the previous question in the list
        /// (Stores the currently selected user answer and moves to the next question in the list)
        /// </summary>
        public void PreviousQuestion()
        {
        }

        /// <summary>
        /// Finish the test by calculating the number of correct answers.
        /// (Check each ExamQuestion.QuestionResult against the ExamQuestions correct answer)
        /// </summary>
        public void Complete()
        {
        }

        /// <summary>
        /// Put the questions from the list of questions in a random order
        /// </summary>
        protected void RandomizeQuestionList()
        {
            int switchIndex = 0;
            System.Random rand = new Random(DateTime.Now.Millisecond);

            for (int numShuffle = 0; numShuffle < 7; numShuffle++)
                for (int i = 0; i < examQuestions.Count; i++)
                {
                    switchIndex = rand.Next(examQuestions.Count);
                    
                    // ensure the switchIndex is not the same as the counter
                    if (switchIndex == i)
                        switchIndex++;

                    ExamQuestion q1 = examQuestions[i];
                    examQuestions[i] = examQuestions[switchIndex];
                    examQuestions[switchIndex] = q1;
                }
        }
    }
}
